#include <linux/skbuff.h>

#include "rxe.h"
#include "rxe_loc.h"
#include "rxe_queue.h"
#include "rxe_task.h"

enum comp_state {
    COMPST_GET_ACK,//0
    COMPST_GET_WQE,//1
    COMPST_COMP_WQE,//2
    COMPST_COMP_ACK,//3
    COMPST_CHECK_PSN,//4
    COMPST_CHECK_ACK,//5
    COMPST_READ,//6
    COMPST_ATOMIC,//7
    COMPST_WRITE_SEND,//8
    COMPST_UPDATE_COMP,//9
    COMPST_ERROR_RETRY,//10
    COMPST_RNR_RETRY,//11
    COMPST_ERROR,//12
    COMPST_EXIT, /* We have an issue, and we want to rerun the completer */
    COMPST_DONE, /* The completer finished successflly */
};

static char *comp_state_name[] = {
    [COMPST_GET_ACK]        = "GET ACK",
    [COMPST_GET_WQE]        = "GET WQE",
    [COMPST_COMP_WQE]       = "COMP WQE",
    [COMPST_COMP_ACK]       = "COMP ACK",
    [COMPST_CHECK_PSN]      = "CHECK PSN",
    [COMPST_CHECK_ACK]      = "CHECK ACK",
    [COMPST_READ]           = "READ",
    [COMPST_ATOMIC]         = "ATOMIC",
    [COMPST_WRITE_SEND]     = "WRITE/SEND",
    [COMPST_UPDATE_COMP]    = "UPDATE COMP",
    [COMPST_ERROR_RETRY]    = "ERROR RETRY",
    [COMPST_RNR_RETRY]      = "RNR RETRY",
    [COMPST_ERROR]          = "ERROR",
    [COMPST_EXIT]           = "EXIT",
    [COMPST_DONE]           = "DONE",
};

static unsigned long rnrnak_usec[32] = {
    [IB_RNR_TIMER_655_36] = 655360,
    [IB_RNR_TIMER_000_01] = 10,
    [IB_RNR_TIMER_000_02] = 20,
    [IB_RNR_TIMER_000_03] = 30,
    [IB_RNR_TIMER_000_04] = 40,
    [IB_RNR_TIMER_000_06] = 60,
    [IB_RNR_TIMER_000_08] = 80,
    [IB_RNR_TIMER_000_12] = 120,
    [IB_RNR_TIMER_000_16] = 160,
    [IB_RNR_TIMER_000_24] = 240,
    [IB_RNR_TIMER_000_32] = 320,
    [IB_RNR_TIMER_000_48] = 480,
    [IB_RNR_TIMER_000_64] = 640,
    [IB_RNR_TIMER_000_96] = 960,
    [IB_RNR_TIMER_001_28] = 1280,
    [IB_RNR_TIMER_001_92] = 1920,
    [IB_RNR_TIMER_002_56] = 2560,
    [IB_RNR_TIMER_003_84] = 3840,
    [IB_RNR_TIMER_005_12] = 5120,
    [IB_RNR_TIMER_007_68] = 7680,
    [IB_RNR_TIMER_010_24] = 10240,
    [IB_RNR_TIMER_015_36] = 15360,
    [IB_RNR_TIMER_020_48] = 20480,
    [IB_RNR_TIMER_030_72] = 30720,
    [IB_RNR_TIMER_040_96] = 40960,
    [IB_RNR_TIMER_061_44] = 61410,
    [IB_RNR_TIMER_081_92] = 81920,
    [IB_RNR_TIMER_122_88] = 122880,
    [IB_RNR_TIMER_163_84] = 163840,
    [IB_RNR_TIMER_245_76] = 245760,
    [IB_RNR_TIMER_327_68] = 327680,
    [IB_RNR_TIMER_491_52] = 491520,
};

static inline unsigned long rnrnak_jiffies(u8 timeout)
{
    return max_t(unsigned long,
        usecs_to_jiffies(rnrnak_usec[timeout]), 1);
}

static enum ib_wc_opcode wr_to_wc_opcode(enum ib_wr_opcode opcode)
{
    switch (opcode) {
    case IB_WR_RDMA_WRITE:
        return IB_WC_RDMA_WRITE;
    case IB_WR_RDMA_WRITE_WITH_IMM:
        return IB_WC_RDMA_WRITE;
    case IB_WR_SEND:
        return IB_WC_SEND;
    case IB_WR_SEND_WITH_IMM:
        return IB_WC_SEND;
    case IB_WR_RDMA_READ:
        return IB_WC_RDMA_READ;
    case IB_WR_ATOMIC_CMP_AND_SWP:
        return IB_WC_COMP_SWAP;
    case IB_WR_ATOMIC_FETCH_AND_ADD:
        return IB_WC_FETCH_ADD;
    case IB_WR_LSO:
        return IB_WC_LSO;
    case IB_WR_SEND_WITH_INV:
        return IB_WC_SEND;
    case IB_WR_RDMA_READ_WITH_INV:
        return IB_WC_RDMA_READ;
    case IB_WR_LOCAL_INV:
        return IB_WC_LOCAL_INV;
    case IB_WR_REG_MR:
        return IB_WC_REG_MR;
    case IB_WR_BIND_MW:
        return IB_WC_BIND_MW;
    default:
        return 0xff;
    }
}

void retransmit_timer(struct timer_list *t)
{
    struct rxe_qp *qp = from_timer(qp, t, retrans_timer);

    if(qp->valid) {


    }
}

void rxe_comp_queue_pkt(struct rxe_qp *qp, struct sk_buff *skb)
{
    int must_sched;

    /*将数据包加入resp_pkts队列*/
    skb_queue_tail(&qp->resp_pkts, skb);

    /*skb队列的数据包个数*/
    must_sched = skb_queue_len(&qp->resp_pkts) > 1;
    if (must_sched != 0)
        rxe_counter_inc(SKB_TO_PKT(skb)->rxe, RXE_CNT_COMPLETER_SCHED);

    /*调度回调函数处理收到的ack包*/
    rxe_run_task(&qp->comp.task, must_sched);
}

static inline enum comp_state get_wqe(struct rxe_qp *qp,
        struct rxe_pkt_info *pkt, struct rxe_send_wqe **wqe_p)
{
    struct rxe_send_wqe *wqe;

    /*当前处理的wqe*/
    wqe = queue_head(qp->sq.queue, QUEUE_TYPE_FROM_CLIENT);
    /*输出参数*/
    *wqe_p = wqe;

    /* no WQE or requester has not started it yet */
    if (!wqe || wqe->state == wqe_state_posted)
        return pkt ? COMPST_DONE : COMPST_EXIT;

    /* wqe的状态已经done了，说明它不需要ack包 */
    if (wqe->state == wqe_state_done)
        return COMPST_COMP_WQE;

    /* WQE caused an error */
    if (wqe->state == wqe_state_error)
        return COMPST_ERROR;

    /*走到这里，什么wqe存在，并且也收到了ack包，下一步就要检查包的psn了*/
    return pkt ? COMPST_CHECK_PSN : COMPST_EXIT;
}

static inline void reset_retry_counters(struct rxe_qp *qp)
{
    qp->comp.retry_cnt = qp->attr.retry_cnt;
    qp->comp.rnr_retry = qp->attr.rnr_retry;
    qp->comp.started_retry = 0;
}

/*这个逻辑没太看懂???*/
static inline enum comp_state check_psn(struct rxe_qp *qp,
        struct rxe_pkt_info *pkt,
        struct rxe_send_wqe *wqe)
{
    s32 diff;

    /*这里的比较，常规情况是小于0或者等于0，
    大于0，说明是send/write已经完成了， 或者是read/atomic发生错误*/
    diff = psn_compare(pkt->psn, wqe->last_psn);
    if(diff > 0) { /*pkt->psn > wqe->last_psn*/


    } else if(diff < 0) {
        printk("diff < 0\n");
    } else { //diff == 0
        printk("diff == 0\n");
    }

    /*compare response packet to expected response*/
    diff = psn_compare(pkt->psn, qp->comp.psn);
    if(diff < 0) {

    } else if() {

    } else {
        /*一般情况下流程是走到这里的*/
        return COMPST_CHECK_ACK;
    }
}

/*ack包检查*/
static inline enum comp_state check_ack(struct rxe_qp *qp,
        struct rxe_pkt_info *pkt,
        struct rxe_send_wqe *wqe)
{
    u8 syn;
    unsigned int mask = pkt->mask;
    struct rxe_dev *rxe = to_rdev(qp->ibqp.device);

    switch(qp->comp.opcode) {
    case -1:
        break;
    case IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST:
    case IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE:
        break;
    default:
        WARN_ON_ONCE(1);
    }

    switch(pkt->opcode) {
    /*收到读请求包*/
    case IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST:
    case IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST:
    case IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY:

    fallthrough;
    case IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE:
        reset_retry_counters(qp);
        return COMPST_READ;

    /*收到atomic ack包*/
    case IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE:
        /*取出syn字段*/
        syn = aeth_syn(pkt);
        if (wqe->wr.opcode != IB_WR_ATOMIC_CMP_AND_SWP &&
            wqe->wr.opcode != IB_WR_ATOMIC_FETCH_AND_ADD)
            return COMPST_ERROR;
        reset_retry_counters(qp);
        return COMPST_ATOMIC;
    /*收到ack包*/
    case IB_OPCODE_RC_ACKNOWLEDGE:
        /*得到syn字段(1个字节,reserved(1bit) + opcode ack(2bit) + credit count(5bit))*/
        syn = aeth_syn(pkt);
        /*ack包分很多种, 取前3bit的值--syn & 1110 0000(0xe0)*/
        switch(syn & AETH_TYPE_MASK) {
        case AETH_ACK://0x00 -- 0000 0000
            reset_retry_counters(qp);
            return COMPST_WRITE_SEND;
        case AETH_RNR_NAK://Receiver Not Ready (RNR) Nak,0x20 -- 0010 0000
            rxe_counter_inc(rxe, RXE_CNT_RCV_RNR);
            return COMPST_RNR_RETRY;
        case AETH_NAK://0x60 -- 0110 0000
            switch (syn) {
            case AETH_NAK_PSN_SEQ_ERROR://包序列号错误,0x60 -- 0110 0000
                return COMPST_ERROR_RETRY;

            /*下面这些错误就没有重传机制了*/
            case AETH_NAK_INVALID_REQ://无效请求,0x61 -- 0110 0001
                wqe->status = IB_WC_REM_INV_REQ_ERR;
                return COMPST_ERROR;
            case AETH_NAK_REM_ACC_ERR://远程访问错误(rkey错误),0x62 -- 0110 0010
                wqe->status = IB_WC_REM_ACCESS_ERR;
                return COMPST_ERROR;
            case AETH_NAK_REM_OP_ERR://远程操作错误,0x63 -- 0110 0110
                wqe->status = IB_WC_REM_OP_ERR;
                return COMPST_ERROR;
            default:
                pr_warn("unexpected nak %x\n", syn);
                wqe->status = IB_WC_REM_OP_ERR;
                return COMPST_ERROR;
            }
        default:
            return COMPST_ERROR;
        }
        break;
    default:
        pr_warn("unexpected opcode\n");
    }
    return COMPST_ERROR;
}

/*读操作*/
static inline enum comp_state do_read(struct rxe_qp *qp,
            struct rxe_pkt_info *pkt, struct rxe_send_wqe *wqe)
{
    int ret;

    ret = copy_data(qp->pd, IB_ACCESS_LOCAL_WRITE,
            &wqe->dma, payload_addr(pkt), payload_size(pkt), RXE_TO_MR_OBJ);
    if (ret) {
        wqe->status = IB_WC_LOC_PROT_ERR;
        return COMPST_ERROR;
    }

    /*所有的读请求包都响应了*/
    if (wqe->dma.resid == 0 && (pkt->mask & RXE_END_MASK))
        return COMPST_COMP_ACK;

    return COMPST_UPDATE_COMP;
}

/*收到回复的atomic ack包*/
static inline enum comp_state do_atomic(struct rxe_qp *qp,
        struct rxe_pkt_info *pkt, struct rxe_send_wqe *wqe)
{
    int ret;

    /*获取远端数据的原始值，无论是cas操作还是faa操作,回ack时的操作都是一样的，
    都是将该值写入到本端下发的地址里*/
    u64 atomic_orig = atmack_orig(pkt);

    ret = copy_data(qp->pd, IB_ACCESS_LOCAL_WRITE,
        &wqe->dma, &atomic_orig, sizeof(u64), RXE_TO_MR_OBJ);
    if(ret) {
        wqe->status = IB_WC_LOC_PROT_ERR;
        return COMPST_ERROR;
    }

    return COMPST_COMP_ACK;
}

/*cqe的生成---细节先不探讨*/
static void make_send_cqe(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
              struct rxe_cqe *cqe)
{
    memset(cqe, 0, sizeof(*cqe));

    if (!qp->is_user) {
        struct ib_wc *wc = &cqe->ibwc;

        wc->wr_id = wqe->wr.wr_id;
        wc->status = wqe->status;
        wc->opcode = wr_to_wc_opcode(wqe->wr.opcode);
        if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
            wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
            wc->wc_flags = IB_WC_WITH_IMM;
        wc->byte_len = wqe->dma.length;
        wc->qp = &qp->ibqp;
    } else {
        struct ib_uverbs_wc *uwc = &cqe->uibwc;

        uwc->wr_id = wqe->wr.wr_id;
        uwc->status = wqe->status;
        uwc->opcode = wr_to_wc_opcode(wqe->wr.opcode);
        if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
            wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
            uwc->wc_flags = IB_WC_WITH_IMM;
        uwc->byte_len = wqe->dma.length;
        uwc->qp_num = qp->ibqp.qp_num;
    }
}

static void do_complete(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
{
    struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
    struct rxe_cqe cqe;
    bool post;

    /* 需要在cq队列里生成cqe */
    post = ((qp->sq_sig_type == IB_SIGNAL_ALL_WR) ||
        (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
        wqe->status != IB_WC_SUCCESS);

    /*开始制作cqe*/
    if(post)
        make_send_cqe(qp, wqe, &cqe);

    /*发送队列cidx移动*/
    queue_advance_consumer(qp->sq.queue, QUEUE_TYPE_FROM_CLIENT);

    /*将cqe拷贝到cq队列里面*/
    if (post)
        rxe_cq_post(qp->scq, &cqe, 0);

    /*计数*/
    if (wqe->wr.opcode == IB_WR_SEND ||
        wqe->wr.opcode == IB_WR_SEND_WITH_IMM ||
        wqe->wr.opcode == IB_WR_SEND_WITH_INV)
        rxe_counter_inc(rxe, RXE_CNT_RDMA_SEND);

    //...

}

static inline enum comp_state complete_ack(struct rxe_qp *qp,
        struct rxe_pkt_info *pkt,
        struct rxe_send_wqe *wqe)
{
    unsigned long flags;





}

static inline enum comp_state complete_wqe(struct rxe_qp *qp,
        struct rxe_pkt_info *pkt,
        struct rxe_send_wqe *wqe)
{



    return COMPST_GET_WQE;
}

static void rxe_drain_resp_pkts(struct rxe_qp *qp, bool notify)
{
    struct sk_buff *skb;
    struct rxe_send_wqe *wqe;
    struct rxe_queue *q = qp->sq.queue;

    /*释放skb队列所有包*/
    while ((skb = skb_dequeue(&qp->resp_pkts))) {
        rxe_drop_ref(qp);
        kfree_skb(skb);
        ib_device_put(qp->ibqp.device);
    }

    //...

}

static void free_pkt(struct rxe_pkt_info *pkt)
{
    struct sk_buff *skb = PKT_TO_SKB(pkt);
    struct rxe_qp *qp = pkt->qp;
    struct ib_device *dev = qp->ibqp.device;

    kfree_skb(skb);
    rxe_drop_ref(qp);
    ib_device_put(dev);
}

int rxe_completer(void *arg)
{
    struct rxe_qp *qp = (struct rxe_qp *)arg;
    struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
    struct rxe_send_wqe *wqe = NULL;
    struct sk_buff *skb = NULL;
    struct rxe_pkt_info *pkt = NULL;
    enum comp_state state;
    int ret = 0;

    rxe_add_ref(qp);

    if (!qp->valid || qp->req.state == QP_STATE_ERROR ||
        qp->req.state == QP_STATE_RESET) {
        rxe_drain_resp_pkts(qp, qp->valid && qp->req.state == QP_STATE_ERROR);
        ret = -EAGAIN;
        goto done;
    }

    if (qp->comp.timeout) {
        qp->comp.timeout_retry = 1;
        qp->comp.timeout = 0;
    } else {
        qp->comp.timeout_retry = 0;
    }

    /**/
    if (qp->req.need_retry) {
        ret = -EAGAIN;
        goto done;
    }

    /*初始状态*/
    state = COMPST_GET_ACK;
    while(1) {
        pr_debug("qp#%d state = %s\n", qp_num(qp), comp_state_name[state]);
        switch(state) {
        /*取出ack包或者读请求包*/
        case COMPST_GET_ACK:
            /*从收包skb链表里取出skb*/
            skb = skb_dequeue(&qp->resp_pkts);
            if(skb) {
                pkt = SKB_TO_PKT(skb);
                qp->comp.timeout_retry = 0;
            }
            state = COMPST_GET_WQE;
            break;

        case COMPST_GET_WQE:
            state = get_wqe(qp, pkt, &wqe);
            break;

        case COMPST_CHECK_PSN:
            state = check_psn(qp, pkt, wqe);
            break;

        case COMPST_CHECK_ACK:
            state = check_ack(qp, pkt, wqe);
            break;

        case COMPST_READ:
            state = do_read(qp, pkt, wqe);
            break;

        case COMPST_ATOMIC:
            state = do_atomic(qp, pkt, wqe);
            break;

        case COMPST_WRITE_SEND:

            break;

        case COMPST_COMP_ACK:
            state = complete_ack(qp, pkt, wqe);
            break;

        case COMPST_COMP_WQE:
            state = complete_wqe(qp, pkt, wqe);
            break;

        case COMPST_UPDATE_COMP:
            break;

        /*正常流程走到这里退出*/
        case COMPST_DONE:
            goto done;

        case COMPST_EXIT:
            goto done;

        case COMPST_ERROR_RETRY:
            break;

        case COMPST_RNR_RETRY: /*收到了nack包*/
            if(qp->comp.rnr_retry > 0) {
                if (qp->comp.rnr_retry != 7)
                    qp->comp.rnr_retry--;
                /*重传标志*/
                qp->req.need_retry = 1;
                pr_debug("qp#%d set rnr nak timer\n", qp_num(qp));
                /**/
                mod_timer(&qp->rnr_nak_timer, jiffies + rnrnak_jiffies(aeth_syn(pkt) & ~AETH_TYPE_MASK));
                ret = -EAGAIN;
                goto done;
            } else {
                rxe_counter_inc(rxe, RXE_CNT_RNR_RETRY_EXCEEDED);
                wqe->status = IB_WC_RNR_RETRY_EXC_ERR;
                state = COMPST_ERROR;
            }
            break;
        case COMPST_ERROR:
            ret = -EAGAIN;
            goto done;
        }
    }
done:
    if(pkt)
        free_pkt(pkt);
    rxe_drop_ref(qp);

    return ret;
}
